package com.goldze.mvvmhabit.ui.tab_bar.fragment;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.goldze.mvvmhabit.R;
import com.goldze.mvvmhabit.adapter.MsgAdapter;
import com.goldze.mvvmhabit.bean.MsgBean;
import com.goldze.mvvmhabit.databinding.FragmentTabBar3Binding;
import com.goldze.mvvmhabit.okgo.HTTPAPI;
import com.goldze.mvvmhabit.utils.GsonUtil;
import com.goldze.mvvmhabit.utils.SpUtils;
import com.lzy.okgo.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.utils.LogUtil;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by goldze on 2018/7/18.
 */

public class TabBar3Fragment extends Fragment {

    FragmentTabBar3Binding binding;
    public MsgAdapter msgAdapter;
    public List<MsgBean.ResultBean> listBeans = new ArrayList<>();


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(LayoutInflater.from(getActivity()), R.layout.fragment_tab_bar_3, container, false);
        binding.setFragment(this);
        initData();
        return binding.getRoot();
    }

    private void initData() {
        binding.rvModule.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.rvModule.setItemAnimator(new DefaultItemAnimator());
        msgAdapter = new MsgAdapter(getActivity(),listBeans);
        binding.rvModule.setAdapter(msgAdapter);

        //推送信息获取
        getMsgList();
    }

    public void getMsgList() {
        String personId = (String) SpUtils.get("module_person_id", "");
        String token = (String) SpUtils.get("module_token","");
        LogUtil.e("personId-->"+ personId);
        HTTPAPI.getInstance().getMsgList(personId, token,new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtil.e("推送信息-->" + s);

                MsgBean msgBean = GsonUtil.INSTANCE.parse(s,MsgBean.class);
                if (msgBean.getCode() == 200) {
                    listBeans.clear();
                    listBeans.addAll(msgBean.getResult());
                    msgAdapter.notifyDataSetChanged();
                }
            }
        });
    }


}
